package com.lanju.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lanju.dao.SharePrizeDao;
import com.lanju.dao.YezhuCaseDao;
import com.lanju.entity.Money;
import com.lanju.entity.SharePrize;
import com.lanju.entity.YezhuCase;
import com.lanju.service.YezhuCaseService;
@Service
public class YezhuCaseServiceImpl implements YezhuCaseService {
	
	@Autowired
	private YezhuCaseDao yezhuCaseDao;
	 @Autowired
	 SharePrizeDao sharePriceDao;

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> addIntroduce(Map<String, Object> map) {
		HashMap<String, Object> result=new HashMap<>();
		try {
			YezhuCase introduce=new YezhuCase();
			introduce.setCreateTime(new Date());
			introduce.setCreator(map.get("userCode").toString());
			introduce.setIntroduce(map.get("introduce").toString());
			introduce.setTitle(map.get("title").toString());
			yezhuCaseDao.addIntroduce(introduce);
			result.put("success", true);
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		return result;
	}
	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> updateIntroduce(Map<String, Object> map) {
		HashMap<String, Object> result=new HashMap<>();
		try {
			YezhuCase introduce=yezhuCaseDao.getIntroduceDetail(Integer.valueOf(map.get("id").toString()));
			introduce.setIntroduce(map.get("introduce").toString());
			introduce.setTitle(map.get("title").toString());
			yezhuCaseDao.updateIntroduce(introduce);
			result.put("success", true);
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		return result;
	}
	

	@Override
	@Transactional(value="financialmarketingTM", readOnly=true)
	public Map<String, Object> getIntroduceDetail(Map<String, Object> map) {
		 Map<String, Object> result=new HashMap<>();
		 YezhuCase i=yezhuCaseDao.getIntroduceDetail(Integer.valueOf(map.get("id").toString()));
			result.put("rows", i);
			return result;
	}

	@Override
	public Map<String, Object> getAllCaseList(Map<String, Object> map) {
		 Map<String, Object> result=new HashMap<>();
		 result.put("rows", yezhuCaseDao.getAllCaseList(map));
		 result.put("total",yezhuCaseDao.getAllCaseListCount(map));
		return result;
	}
	
	@Override
	public Map<String, Object> getCleanCard(String phone) {
		Map<String, Object> result=new HashMap<>();
		List<SharePrize> list=sharePriceDao.hasSharePrize(phone);
		SharePrize sp=null;
		if(list !=null && list.size()>0){
			sp = list.get(0);
			try {
				Money m=sharePriceDao.hasMoney(phone,1);
				result.put("money",m.getPrice());
			} catch (Exception e) {
				
				e.printStackTrace();
			}
		}else{
			result.put("money", 0);
		}
		return result;
	}

}
